package com.controller;


import com.entity.BUser;
import com.entity.FUser;
import com.entity.table.BUserTableDef;
import com.mapper.BUserMapper;
import com.mapper.FUserMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.impl.BUserServiceImpl;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.swing.*;


@Controller
@RequestMapping("/admin")
public class AdminController {
    @GetMapping("")
    public String index(Model model){
        return "back/index";
    }
    @GetMapping("/welcome")
    public String welcome(Model model){
        return "back/welcome";
    }
    @Autowired
    BUserServiceImpl bUserService;
    @GetMapping("/login")
    public String toLoginPage(){
        return "back/login";
    }
    @PostMapping("/toLogin")//提交登录，登录成功跳转到后台首页
    public String login(BUser user, HttpSession httpSession, Model model) {
        QueryWrapper query=QueryWrapper.create().where(BUserTableDef.B_USER.USERNAME.eq(user.getUsername()))
                .and(BUserTableDef.B_USER.PASSWORD.eq(user.getPassword()));
        BUser buser = bUserService.getOne(query);
        if (buser != null) {
            httpSession.setAttribute("admin", buser);//将用户对象添加到Session
            model.addAttribute("admin", buser);
            model.addAttribute("msg", "账号或密码错误，请重新输入！");
            return "back/index";
            // return "redirect:/index";//跳转到后台首页
        } else {
            model.addAttribute("msg", "账号或密码错误，请重新输入！");
            return "back/login";
        }
    }
}
